package yxk.utils;

import kd.bos.coderule.api.CodeRuleInfo;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.fileservice.FileItem;
import kd.bos.fileservice.FileService;
import kd.bos.fileservice.FileServiceFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.AttDto;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.servicehelper.coderule.CodeRuleServiceHelper;

import java.io.InputStream;
import java.util.*;

public class BosUtil {
    private static Log logger = LogFactory.getLog(BosUtil.class);
    /*
    * number 单据编码
    *根据编码获取接口配置地址的数据
    * */
    public static DynamicObject getConfigUration(String number){
        QFilter qFilter=new QFilter("status", QCP.equals,"C")
                .and(new QFilter("enable", QCP.equals,'1'))
                        .and(new QFilter("number",QCP.equals,number));
        DynamicObject single = BusinessDataServiceHelper.loadSingle("nwg5_configuration",
                "number,name,nwg5_url,nwg5_tokenurl,nwg5_secreturl,,nwg5_user,nwg5_password", qFilter.toArray());
        return single;
    }


    /**
     *  上传附件
     *
     * @param fileStream    附件流
     * @param srcbillo      上传的单据
     * @param name          附件名称
     * @param attname       附件面板名称
     * @throws Exception
     */
    public static void setAttachment(InputStream fileStream, DynamicObject srcbillo, String name, String attname) throws Exception{
        if(srcbillo==null){
            return;
        }
        DynamicObject srcbill = BusinessDataServiceHelper.loadSingle(srcbillo.getPkValue(), srcbillo.getDynamicObjectType().getName());
        Map<String,Object> params=new HashMap();
        params.put("formNumber",srcbill.getDynamicObjectType().getName());// 目标单据标识
        params.put("billPkId",srcbill.getPkValue());//目标单据PkId
        Map<String,Object> map=new HashMap();
        List<AttDto> list=new ArrayList<>();

        FileService fs= FileServiceFactory.getAttachmentFileService();
        String path = "/"+srcbill.getDynamicObjectType().getName()+"/attachmentpanel/"+name;
        int size=fileStream.available();
        logger.info("size:"+size);
        FileItem fi = new FileItem(name,path,fileStream);

        fi.setCreateNewFileWhenExists(true);
        String paths= fs.upload(fi);
        //获取实际存储路径
        String realPath = FileServiceFactory.getAttachmentFileService().getFileServiceExt().getRealPath(paths);
        logger.info("==realPath=="+realPath);
        logger.info("path:"+paths);
        AttacUpdate(srcbillo,name,realPath,size,attname);
    }

    /**
     * 更新附件面板
     *
     * @param pkid        单据ID
     * @param fileName    附件名称
     * @param uploadPath  上传地址
     * @param size        文件大小
     * @param attname     上传附件面板的标识
     */
    public static void AttacUpdate(DynamicObject pkid,String fileName,String uploadPath,int size,String attname){
        Map uploadFileMap = new HashMap();
        uploadFileMap.put("entityNum", pkid.getDynamicObjectType().getName());
        uploadFileMap.put("billPkId", pkid.toString());
        uploadFileMap.put("lastModified", new Date().getTime());

        StringBuffer uid = new StringBuffer();
        long time = new Date().getTime();
        uid.append("rc-upload-");
        uid.append(time);
        uid.append("-");
        uid.append("1");
        uploadFileMap.put("uid", uid.toString());
        uploadFileMap.put("name", fileName);
        uploadFileMap.put("url", uploadPath);
        uploadFileMap.put("size", size);
        uploadFileMap.put("status", "success");

        List<Map<String, Object>> attachments = new ArrayList<>();
        attachments.add(uploadFileMap);

        AttachmentServiceHelper.upload(pkid.getDynamicObjectType().getName(), pkid.getPkValue(), attname, attachments);

    }

    /**
     *
     * @param sl 单据
     * @return获取单据编码规则
     */
    public static String getNumber(DynamicObject sl){
        CodeRuleInfo codeRule = CodeRuleServiceHelper.getCodeRule(sl.getDataEntityType().getName(), sl, null);
        return CodeRuleServiceHelper.getNumber(codeRule, sl);
    }

}
